﻿@{
    ViewData["Title"] = "Index";
    Layout = "~/Areas/Console/Views/Shared/_Layout.cshtml";
}
@section head{
    <script type="text/javascript">
        var table;
        layui.config({
            base: '../../layuiadmin/' //静态资源所在路径
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index', 'table', 'form'], function () {
            var $ = layui.$
                , form = layui.form
                , admin = layui.admin
                , table = layui.table;

            table.render({
                elem: '#table-datalist'
                , url: '/Console/Power/GetListJson?menuid=@ViewBag.menuid'
                , title: '权限数据表'
                , cols: [[
                    { type: 'checkbox', fixed: 'left' }
                    , { field: 'names', title: '权限名称', width: 100 }
                    , { field: 'controller', title: 'Controller', width: 100 }
                    , { field: 'action', title: 'Action', width: 100 }
                    , { field: 'creator', title: '创建人', width: 100 }
                    , { field: 'createDate', title: '添加时间', width: 100 }
                    , { field: 'sort', title: '排序', width: 80 }
                    , { field: 'remark', title: '备注' }
                    , { fixed: 'right', title: '操作', toolbar: '#table-datalist-bar' }
                ]]
                , page: false
            });

            //监听行工具事件
            table.on('tool(table-datalist)', function (obj) {
                var data = obj.data;
                if (obj.event === 'del') {
                    layer.confirm('确定要删除此权限吗？', function (index) {
                        $.ajax({
                            type: "POST",
                            url: "/Console/Power/Delete",
                            data: { id: data.id }, // 你的formid
                            async: true,
                            error: function (request) {
                                alert("Connection error");
                            },
                            beforeSend: function (xhr) {
                                layui.layer.load();
                            },
                            success: function (data) {
                                var item = eval(data);
                                if (item.code == 0) {
                                    layer.msg("删除成功", { icon: 1, time: 1000 }, function () {
                                        table.reload("table-datalist");
                                    });
                                } else if (item.code == 1) {
                                    layer.msg(item.msg, { icon: 2 });
                                } else {
                                    layer.msg("删除失败", { icon: 0 });
                                }
                                layer.closeAll('loading');
                            }
                        });
                    });
                } else if (obj.event === 'edit') {
                    var index = layer.open({
                        type: 2,
                        title: '编辑权限',
                        shadeClose: true,
                        shade: 0.8,
                        area: ['800px', '600px'],
                        content: '/Console/Power/Edit?id=' + data.id
                    });
                    layer.full(index);
                }
            });
            form.on('submit(addNew)', function (data) {
                var index = layer.open({
                    type: 2,
                    title: '新增权限',
                    shadeClose: true,
                    shade: 0.8,
                    area: ['800px', '600px'],
                    content: '/Console/Power/Create?menuid=@ViewBag.menuid'
                });
                layer.full(index);
            });
            form.on('submit(DelItems)', function (data) {
                var checkStatus = table.checkStatus('table-datalist');
                var data = checkStatus.data;
                if (data.length > 0) {
                    var ids = "";
                    for (var i = 0; i < data.length; i++) {
                        if (ids == "")
                            ids = data[i].id;
                        else
                            ids += "," + data[i].id;
                    }
                    if (ids != "") {
                        layer.confirm('确定要删除选中的权限吗？', function (index) {
                            $.ajax({
                                type: "POST",
                                url: "/Console/Power/DeleteItems",
                                data: { ids: ids }, // 你的formid
                                async: true,
                                error: function (request) {
                                    alert("Connection error");
                                },
                                beforeSend: function (xhr) {
                                    layui.layer.load();
                                },
                                success: function (data) {
                                    var item = eval(data);
                                    if (item.code == 0) {
                                        layer.msg("删除成功", { icon: 1, time: 1000 }, function () {
                                            table.reload("table-datalist");
                                        });
                                    } else if (item.code == 1) {
                                        layer.msg(item.msg, { icon: 2 });
                                    } else {
                                        layer.msg("删除失败", { icon: 2 });
                                    }
                                    layer.closeAll('loading');
                                }
                            });
                        });
                    } else {
                        layer.msg("请选择需要删除的权限", { icon: 0 });
                    }
                } else {
                    layer.msg("请选择需要删除的权限", { icon: 0 });
                }
            });
        });
    </script>
}
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">权限管理</div>
                <div class="layui-card-body">
                    <div class="layui-table-tool layui-form" style="box-sizing: border-box;">
                        <div class="layui-table-tool-temp">
                            <button type="button" class="layui-btn layui-btn-sm" id="addNew" lay-submit lay-filter="addNew">新增</button>
                            <button type="button" class="layui-btn layui-btn-sm" id="DelItems" lay-submit lay-filter="DelItems">批量删除</button>
                        </div>
                    </div>
                    <table class="layui-hide" id="table-datalist" lay-filter="table-datalist"></table>
                    <script type="text/html" id="table-datalist-bar">
                        <button class="layui-btn layui-btn-xs" lay-event="edit">编辑</button>
                        <button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</button>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>